<?php
/**
 * 图片站点
 * ***********************
 * * Author:蜗牛他大哥    *
 * * Time: 2018-05-23    *
 * * QQ交流群：101766150  *
 * ***********************
 * */
namespace app\img\controller;

use think\Controller;
use think\Model;
use think\db;
use think\Cache;



class ImgSite extends ImgBase
{
    protected $model;
    protected $imgModel;
    protected $folderModel;
    protected $siteModel;
    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = Model('common/imgSite');
        $this->folderModel = Model('common/imgFolder');
        $this->imgModel = Model('common/img');
        $this->siteModel = Model('common/imgSite');
    }

    public function index()
    {
        //$s = $this->model->imgSitedata();
        //print_r($s);
        //Cache::clear();

        $res = $this->model->imgSiteCache();

        // 格式化数据
        if($res){
            foreach($res as $k=>$v){
                $where['site'] =$v['id'];
                $res[$k]['img_count'] = $this->imgModel->comCount($where);

            }
        }

        $this->assign('res',$res);
        return $this->fetch();
    }

    // ajaxAdd
    public function ajaxDoAdd(){
        isAjaxText();
        $name = trim(input('name'));
        $domain = trim(input('domain'));

        $code =100;
        $msg = '失败';
        $data['name'] = $name;
        $data['domain'] = $domain;
        // 文件夹名称命名
        $data['folder'] = date('Ymd').rand(1000,9999);
        $data['add_time'] = time();
        //$res = $this->model->comSave($data);

        $res = db('imgSite')->insert($data);
        $siteId = db('imgSite')->getLastInsID();

        if($res==1){
            $code =200;
            $msg = '成功';

            // 建立默认文件夹
            $folder['pid']           = 0;
            $folder['name']          = '目录';
            $folder['is_delete']     = 1;
            $folder['add_time']      = time();
            $folder['site']          = $siteId;

            $this->folderModel->comSave($folder);

            // 重新缓存 站点数据
            $this->model->imgSitedata();
            // 重新缓存 文件夹数据
            $this->folderModel->folderData();
            // 创建物理目录
            $this->folderModel->mkdirFolder($data['folder']);
        }

        $result['code'] = $code;
        $result['msg'] = $msg;
        return $result;
    }
    //
    public function ajaxDoEdit(){
        isAjaxText();
        $data['id']           = intval(input('id'));
        $data['name']         = trim(input('name'));
        $data['domain']       = trim(input('domain'));

        $res= $this->model->comSave($data,$data['id'] );
        if($res){
            $result['code'] =200;
            $result['msg'] ='成功';
            // 重新缓存数据
            $this->model->imgSitedata();
        }else{
            $result['code'] =100;
            $result['msg'] ='失败';
        }
        return $result;

    }

    // ajaxInfo
    public function ajaxInfo(){
        isAjaxText();
        $id = intval(input('id'));
        $res = $this->model->cacheSearch($id);
        if($res){
            $result['code'] =200;
            $result['msg'] ='成功';
            $result['data'] = $res;
        }else{
            $result['code'] =100;
            $result['msg'] ='失败';
        }
        return $result;
    }


    /**
     * 切换站点
     * @param int $siteid 图片站点id(如果为空，默认读取第一个站点)
     * @return int array
     * */
    public function changeSite(){
        isAjaxText();
        $siteId = intval(input('id'));
        if($siteId){
            //a.设置session
            session('imgSite',$siteId);
            //b.返回默认目录id
            $res = $this->folderModel->siteData();
            $result['code'] = 200;
            $result['msg'] = '成功';
            $result['data']['folder'] = $res[0];
        }else{
            $result['code'] = 201;
            $result['msg'] = '参数出错';
        }
        return $result;
        //print_r($res);

    }
    // 清除站点数据
    public function clearSite(){
        $id = intval(input('id'));

        Db::startTrans();
        try{
            $where['site'] = $id;
            // 删除图片
            $this->imgModel->comDestroy($where);
            // 删除目录
            $where['pid'] =['neq',0];   // 主目录除外(不删除)
            $this->folderModel->comDestroy($where);
            // 提交事务
            Db::commit();
            $json['code'] = 200;
            $json['msg'] = '成功';
        }catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            $json['code'] = 100;
            $json['msg'] = '失败';
        }

        // 重新生成缓存
        $this->folderModel->folderData();

        return $json;
    }
    // 删除站点
    public function deleteSite(){
        isAjaxText();
        $id = intval(input('id'));

        //a.检测是否存在图片
        $where['site'] = $id;
        $count = $this->imgModel->comCount($where);

        if($count>0){
            $code = 101;
            $msg = '删除失败，站点下存在图片';
        }else{
            // 删除站点
            $res = $this->siteModel->comDestroy($id);
            // 删除目录
            $this->folderModel->comDestroy($where);
            if($res>0){
                $code = 200;
                $msg = '成功';
                // 重新生成缓存
                $this->siteModel->imgSitedata();
                $this->folderModel->folderData();
            }else{
                $code = 100;
                $msg = '失败';
            }
        }
        $json['code'] = $code;
        $json['msg'] = $msg;
        return $json;
    }

}
